package cn.com.stary1993.kite.captcha.support.behavior;


import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.generator.common.constant.SliderCaptchaConstant;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import cloud.tianai.captcha.resource.common.model.dto.ResourceMap;
import cloud.tianai.captcha.resource.impl.DefaultResourceStore;
import cloud.tianai.captcha.resource.impl.provider.ClassPathResourceProvider;
import cn.com.stary1993.kite.captcha.config.BehaviorImageCaptchaProperties;
import cn.hutool.core.collection.CollectionUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator.DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH;

/**
 * @author stary1993
 * @date 2022-07-09
 */
@Slf4j
@Data
public class BehaviorImageCaptchaResourceStore extends DefaultResourceStore {

	/**
	 * 默认资源类型
	 */
	private static final String DEFAULT_RESOURCE_TYPE = ClassPathResourceProvider.NAME;

	/**
	 * 默认资源路径
	 */
	private static final String DEFAULT_RESOURCE_PATH = "META-INF/cut-image/resource/";

	/**
	 * 默认模板路径
	 */
	private static final String DEFAULT_TEMPLATE_PATH = "META-INF/cut-image/template/";

	/**
	 * 默认资源数据
	 */
	private static List<String> DEFAULT_RESOURCE_DATA_LIST = Arrays.asList("a.jpg", "b.jpg", "c.jpg", "d.jpg", "e.jpg",
			"f.jpg", "g.jpg", "h.jpg", "i.jpg");

	private final BehaviorImageCaptchaProperties prop;

	public BehaviorImageCaptchaResourceStore(BehaviorImageCaptchaProperties prop) {
		this.prop = prop;
		// 旋转验证码 模板 (系统内置)
		ResourceMap defaultRotateTemplate = new ResourceMap("default", 4);
		defaultRotateTemplate.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(
				ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/3/active.png")));
		defaultRotateTemplate.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME, new Resource(
				ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/3/fixed.png")));
		addTemplate(CaptchaTypeConstant.ROTATE, defaultRotateTemplate);

		// // 滑块验证码 模板 (系统内置)
		ResourceMap defaultSliderTemplate1 = new ResourceMap("default", 4);
		defaultSliderTemplate1.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(
				ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/1/active.png")));
		defaultSliderTemplate1.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME, new Resource(
				ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/1/fixed.png")));
		addTemplate(CaptchaTypeConstant.SLIDER, defaultSliderTemplate1);
		ResourceMap defaultSliderTemplate2 = new ResourceMap("default", 4);
		defaultSliderTemplate2.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(
				ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/2/active.png")));
		defaultSliderTemplate2.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME, new Resource(
				ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/2/fixed.png")));
		addTemplate(CaptchaTypeConstant.SLIDER, defaultSliderTemplate2);

		List<BehaviorImageCaptchaProperties.Resource> resourceList = prop.getResourceList();
		if (CollectionUtil.isEmpty(resourceList)) {
			// 添加默认背景图片
			DEFAULT_RESOURCE_DATA_LIST.forEach(data -> {
				addResource(CaptchaTypeConstant.SLIDER,
						new Resource(DEFAULT_RESOURCE_TYPE, DEFAULT_RESOURCE_PATH.concat(data), "default"));
				addResource(CaptchaTypeConstant.ROTATE,
						new Resource(DEFAULT_RESOURCE_TYPE, DEFAULT_RESOURCE_PATH.concat(data), "default"));
				addResource(CaptchaTypeConstant.CONCAT,
						new Resource(DEFAULT_RESOURCE_TYPE, DEFAULT_RESOURCE_PATH.concat(data), "default"));
				addResource(CaptchaTypeConstant.WORD_IMAGE_CLICK,
						new Resource(DEFAULT_RESOURCE_TYPE, DEFAULT_RESOURCE_PATH.concat(data), "default"));
				addResource(CaptchaTypeConstant.WORD_ORDER_IMAGE_CLICK,
						new Resource(DEFAULT_RESOURCE_TYPE, DEFAULT_RESOURCE_PATH.concat(data), "default"));
			});

		}
		else {
			// 添加自定义背景图片
			resourceList.forEach(resource -> addResource(resource.getModel().name(),
					new Resource(resource.getType().name, resource.getPath(), "default")));
		}
		// 清除默认模板
		if (prop.getClearAllTemplates()) {
			clearTemplates(CaptchaTypeConstant.SLIDER);
		}

		// 添加自定义模板资源
		List<Map<BehaviorImageCaptchaProperties.TemplateType, BehaviorImageCaptchaProperties.Resource>> templateList = prop
			.getTemplateList();
		if (CollectionUtil.isEmpty(templateList)) {
			ResourceMap template1 = new ResourceMap("default", 4);
			template1.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME,
					new Resource(ClassPathResourceProvider.NAME, DEFAULT_TEMPLATE_PATH.concat("fish/active.png")));
			template1.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME,
					new Resource(ClassPathResourceProvider.NAME, DEFAULT_TEMPLATE_PATH.concat("fish/fixed.png")));
			addTemplate(CaptchaTypeConstant.SLIDER, template1);
			ResourceMap template2 = new ResourceMap("default", 4);
			template2.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME,
					new Resource(ClassPathResourceProvider.NAME, DEFAULT_TEMPLATE_PATH.concat("heart/active.png")));
			template2.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME,
					new Resource(ClassPathResourceProvider.NAME, DEFAULT_TEMPLATE_PATH.concat("heart/fixed.png")));

			addTemplate(CaptchaTypeConstant.SLIDER, template2);
			ResourceMap template3 = new ResourceMap("default", 4);
			template3.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME,
					new Resource(ClassPathResourceProvider.NAME, DEFAULT_TEMPLATE_PATH.concat("like/active.png")));
			template3.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME,
					new Resource(ClassPathResourceProvider.NAME, DEFAULT_TEMPLATE_PATH.concat("like/fixed.png")));
			addTemplate(CaptchaTypeConstant.SLIDER, template3);
		}
		else {
			templateList.forEach(template -> {
				ResourceMap templ = new ResourceMap("custom", 4);
				templ.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME,
						new Resource(template.get(BehaviorImageCaptchaProperties.TemplateType.active).getType().name,
								template.get(BehaviorImageCaptchaProperties.TemplateType.active).getPath()));
				templ.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME,
						new Resource(template.get(BehaviorImageCaptchaProperties.TemplateType.fixed).getType().name,
								template.get(BehaviorImageCaptchaProperties.TemplateType.fixed).getPath()));
				addTemplate(template.get(BehaviorImageCaptchaProperties.TemplateType.active).getModel().name(), templ);
			});
		}

	}

}
